//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Cap.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Instruments/CapFloor.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCap::CCap (Cephei::IVector<Cephei::QL::ICashFlow^>^ floatingLeg, Cephei::IVector<Double>^ exerciseRates, Cephei::QL::IPricingEngine^ QL_Pricer) : CCapFloor(CCap::typeid)
{
    CoVector<Cephei::QL::ICashFlow^>^ _CfloatingLeg;
    CoVector<Double>^ _CexerciseRates;
    try
    {
#ifdef HANDLE
        _phCap = NULL;
#endif
        _CfloatingLeg = safe_cast<CoVector<Cephei::QL::ICashFlow^>^> (floatingLeg);
        _CfloatingLeg ->Lock ();
        INativeVector<Cephei::QL::ICashFlow^>^ _NCIfloatingLeg = _CfloatingLeg->getFeature (NativeFeature::shared_ptr);
        CCashFlowVector^ _NCfloatingLeg = safe_cast<CCashFlowVector^>(_NCIfloatingLeg);
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _floatingLeg = static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCfloatingLeg->GetShared ());
        CoVector<Double>^ _CexerciseRates = safe_cast<CoVector<Double>^> (exerciseRates);
        _CexerciseRates->Lock();
        INativeVector<Double>^ _NCIexerciseRates = _CexerciseRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseRates = safe_cast<CDoubleVector^>(_NCIexerciseRates);
        std::vector<QuantLib::Rate>& _exerciseRates = static_cast<std::vector<QuantLib::Rate>&> (_NCexerciseRates->GetReference ());
        _ppCap = new boost::shared_ptr<QuantLib::Cap> (new QuantLib::Cap ( _floatingLeg,  _exerciseRates ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCap)->setPricingEngine (_QL_Pricer);
        SetCapFloor (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfloatingLeg != nullptr) _CfloatingLeg->Unlock();
        if (_CexerciseRates != nullptr) _CexerciseRates->Unlock();
    }
}
Cephei::QL::Instruments::CCap::CCap (boost::shared_ptr<QuantLib::Cap>& childNative, Object^ owner) : CCapFloor(CCap::typeid)
{
#ifdef HANDLE
	_phCap = NULL;
#endif
	_ppCap = &childNative;
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCap));
}
Cephei::QL::Instruments::CCap::CCap (QuantLib::Cap& childNative, Object^ owner) : CCapFloor(CCap::typeid)
{
#ifdef HANDLE
	_phCap = NULL;
#endif
	_ppCap = new boost::shared_ptr<QuantLib::Cap> (&childNative);
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCap));
    _CapOwner = owner;
    _CapFloorOwner = owner;
}

Cephei::QL::Instruments::CCap::CCap (CCap^ copy) : CCapFloor(CCap::typeid)
{
#ifdef HANDLE
	_phCap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCap = new boost::shared_ptr<QuantLib::Cap> (copy->GetShared());
        _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCap));
    }
}
Cephei::QL::Instruments::CCap::CCap (System::Type^ t) : CCapFloor(CCap::typeid)
{
#ifdef HANDLE
	_phCap = NULL;
#endif
	if (!t->IsSubclassOf(CCap::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCap::CCap (QuantLib::Handle<QuantLib::Cap>& childNative, Object^ owner)  : CCapFloor(CCap::typeid)
{
	_phCap = &childNative;
	_ppCap = &static_cast<boost::shared_ptr<QuantLib::Cap>>(childNative.currentLink());
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCap));
    _CapOwner = owner;
}
Cephei::QL::Instruments::CCap::CCap (QuantLib::Handle<QuantLib::Cap> childNative)  : CCapFloor(CCap::typeid)
{
	_phCap = &childNative;
	_ppCap = &static_cast<boost::shared_ptr<QuantLib::Cap>>(childNative.currentLink());
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCap));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCap::CCap (QuantLib::Cap childNative)  : CCapFloor(CCap::typeid)
{
#ifdef HANDLE
	_phCap = NULL;
#endif
	_ppCap = new boost::shared_ptr<QuantLib::Cap> (new QuantLib::Cap (childNative));
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCap));
}
#endif

Cephei::QL::Instruments::CCap::~CCap ()
{
    if (_ppCap != NULL)
    {
	    delete _ppCap;
        _ppCap = NULL;
    }
}
Cephei::QL::Instruments::CCap::!CCap ()
{
    if (_ppCap != NULL)
    {
	    delete _ppCap;
    }
}
QuantLib::Cap& Cephei::QL::Instruments::CCap::GetReference ()
{
    if (_ppCap == NULL) throw gcnew NativeNullException ();
	return **_ppCap;
}
boost::shared_ptr<QuantLib::Cap>& Cephei::QL::Instruments::CCap::GetShared ()
{
    if (_ppCap == NULL) throw gcnew NativeNullException ();
	return *_ppCap;
}
QuantLib::Cap* Cephei::QL::Instruments::CCap::GetPointer ()
{
    if (_ppCap == NULL) throw gcnew NativeNullException ();
	return &**_ppCap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Cap>& Cephei::QL::Instruments::CCap::GetHandle ()
{
	if (_phCap == NULL)
	{
		_phCap = new Handle<QuantLib::Cap> (*_ppCap);
	}
	return *_phCap;
}
#endif
bool Cephei::QL::Instruments::CCap::HasNative () 
{
	return (_ppCap != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ICap^ Cephei::QL::Instruments::CCap_Factory::Create (Cephei::IVector<Cephei::QL::ICashFlow^>^ floatingLeg, Cephei::IVector<Double>^ exerciseRates, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CCap ( floatingLeg,  exerciseRates,  QL_Pricer);
}
